import { ResponseResult } from "@/entity/ResponseResult";
import { AxiosResponse } from "axios";
import httpClient from "./http-client";

export class FileService {
    baseUrl: string = "/api/v1.0/portal/file";

    getUploadUrl() {
        return `${this.baseUrl}/upload`;
    }

    async upload(file: File): Promise<string> {
        let formData = new FormData();
        formData.append("file", file);
        return httpClient.post(this.getUploadUrl(), formData, true, {
            headers: {
                "Content-Type": "multipart/form-data;charset=utf-8",
            }
        }).then((res: AxiosResponse<ResponseResult<string>>) => {
            if (res.data.data) {
                return Promise.resolve(res.data.data);
            } else {
                throw "no data";
            }
        });
    }

}

const instance = new FileService();
export default instance;